home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / clang / gnumake.zip / MAKE-STD.TEX < prev    next >
Text File  |  1993-08-12  |  20KB  |  529 lines

  1. @comment This file is included by both standards.texi and make.texinfo.
  2. @comment It was broken out of standards.texi on 1/6/93 by roland.
  3.  
  4. @node Makefile Conventions
  5. @chapter Makefile Conventions
  6. @comment standards.texi does not print an index, but make.texinfo does.
  7. @cindex makefile, conventions for
  8. @cindex conventions for makefiles
  9. @cindex standards for makefiles
  10.  
  11. This chapter describes conventions for writing the Makefiles for GNU programs.
  12.  
  13. @menu
  14. * Makefile Basics::
  15. * Utilities in Makefiles::
  16. * Standard Targets::
  17. * Command Variables::
  18. * Directory Variables::
  19. @end menu
  20.  
  21. @node Makefile Basics
  22. @section General Conventions for Makefiles
  23.  
  24. Every Makefile should contain this line:
  25.  
  26. @example
  27. SHELL = /bin/sh
  28. @end example
  29.  
  30. @noindent
  31. to avoid trouble on systems where the @code{SHELL} variable might be
  32. inherited from the environment.  (This is never a problem with GNU
  33. @code{make}.)
  34.  
  35. Don't assume that @file{.} is in the path for command execution.  When
  36. you need to run programs that are a part of your package during the
  37. make, please make sure that it uses @file{./} if the program is built as
  38. part of the make or @file{$(srcdir)/} if the file is an unchanging part
  39. of the source code.  Without one of these prefixes, the current search
  40. path is used.  
  41.  
  42. The distinction between @file{./} and @file{$(srcdir)/} is important
  43. when using the @samp{--srcdir} option to @file{configure}.  A rule of
  44. the form:
  45.  
  46. @smallexample
  47. foo.1 : foo.man sedscript
  48.         sed -e sedscript foo.man > foo.1
  49. @end smallexample
  50.  
  51. @noindent
  52. will fail when the current directory is not the source directory,
  53. because @file{foo.man} and @file{sedscript} are not in the current
  54. directory.
  55.  
  56. When using GNU @code{make}, relying on @samp{VPATH} to find the source
  57. file will work in the case where there is a single dependency file,
  58. since the @file{make} automatic variable @samp{$<} will represent the
  59. source file wherever it is.  (Many versions of @code{make} set @samp{$<}
  60. only in implicit rules.)  A makefile target like
  61.  
  62. @smallexample
  63. foo.o : bar.c
  64.         $(CC) -I. -I$(srcdir) $(CFLAGS) -c bar.c -o foo.o
  65. @end smallexample
  66.  
  67. @noindent
  68. should instead be written as
  69.  
  70. @smallexample
  71. foo.o : bar.c
  72.         $(CC) $(CFLAGS) $< -o $@@
  73. @end smallexample
  74.  
  75. @noindent
  76. in order to allow @samp{VPATH} to work correctly.  When the target has
  77. multiple dependencies, using an explicit @samp{$(srcdir)} is the easiest
  78. way to make the rule work well.  For example, the target above for
  79. @file{foo.1} is best written as:
  80.  
  81. @smallexample
  82. foo.1 : foo.man sedscript
  83.         sed -s $(srcdir)/sedscript $(srcdir)/foo.man > foo.1
  84. @end smallexample
  85.  
  86. @node Utilities in Makefiles
  87. @section Utilities in Makefiles
  88.  
  89. Write the Makefile commands (and any shell scripts, such as
  90. @code{configure}) to run in @code{sh}, not in @code{csh}.  Don't use any
  91. special features of @code{ksh} or @code{bash}.
  92.  
  93. The @code{configure} script and the Makefile rules for building and
  94. installation should not use any utilities directly except these:
  95.  
  96. @example
  97. cat cmp cp echo egrep expr grep
  98. ln mkdir mv pwd rm rmdir sed test touch
  99. @end example
  100.  
  101. Stick to the generally supported options for these programs.  For
  102. example, don't use @samp{mkdir -p}, convenient as it may be, because
  103. most systems don't support it.
  104.  
  105. The Makefile rules for building and installation can also use compilers
  106. and related programs, but should do so via @code{make} variables so that the
  107. user can substitute alternatives.  Here are some of the programs we
  108. mean:
  109.  
  110. @example
  111. ar bison cc flex install ld lex
  112. make makeinfo ranlib texi2dvi yacc
  113. @end example
  114.  
  115. When you use @code{ranlib}, you should test whether it exists, and run
  116. it only if it exists, so that the distribution will work on systems that
  117. don't have @code{ranlib}.
  118.  
  119. If you use symbolic links, you should implement a fallback for systems
  120. that don't have symbolic links.
  121.  
  122. It is ok to use other utilities in Makefile portions (or scripts)
  123. intended only for particular systems where you know those utilities to
  124. exist.
  125.  
  126. @node Standard Targets
  127. @section Standard Targets for Users
  128.  
  129. All GNU programs should have the following targets in their Makefiles:
  130.  
  131. @table @samp
  132. @item all
  133. Compile the entire program.  This should be the default target.  This
  134. target need not rebuild any documentation files; Info files should
  135. normally be included in the distribution, and DVI files should be made
  136. only when explicitly asked for.
  137.  
  138. @item install
  139. Compile the program and copy the executables, libraries, and so on to
  140. the file names where they should reside for actual use.  If there is a
  141. simple test to verify that a program is properly installed, this target
  142. should run that test.
  143.  
  144. The commands should create all the directories in which files are to be
  145. installed, if they don't already exist.  This includes the directories
  146. specified as the values of the variables @code{prefix} and
  147. @code{exec_prefix}, as well as all subdirectories that are needed.
  148. One way to do this is by means of an @code{installdirs} target
  149. as described below.
  150.  
  151. Use @samp{-} before any command for installing a man page, so that
  152. @code{make} will ignore any errors.  This is in case there are systems
  153. that don't have the Unix man page documentation system installed.
  154.  
  155. The way to install Info files is to copy them into @file{$(infodir)}
  156. with @code{$(INSTALL_DATA)} (@pxref{Command Variables}), and then run
  157. the @code{install-info} program if it is present.  @code{install-info}
  158. is a script that edits the Info @file{dir} file to add or update the
  159. menu entry for the given Info file; it will be part of the Texinfo package.
  160. Here is a sample rule to install an Info file:
  161.  
  162. @comment This example has been carefully formatted for the Make manual.
  163. @comment Please do not reformat it without talking to roland@gnu.ai.mit.edu.
  164. @smallexample
  165. $(infodir)/foo.info: foo.info
  166. # There may be a newer info file in . than in srcdir.
  167.         -if test -f foo.info; then d=.; \
  168.          else d=$(srcdir); fi; \
  169.         $(INSTALL_DATA) $$d/foo.info $@@; \
  170. # Run install-info only if it exists.
  171. # Use `if' instead of just prepending `-' to the
  172. # line so we notice real errors from install-info.
  173. # We use `$(SHELL) -c' because some shells do not
  174. # fail gracefully when there is an unknown command.
  175.         if $(SHELL) -c 'install-info --version' \
  176.            >/dev/null 2>&1; then \
  177.           install-info --infodir=$(infodir) $$d/foo.info; \
  178.         else true; fi
  179. @end smallexample
  180.  
  181. @item uninstall
  182. Delete all the installed files that the @samp{install} target would
  183. create (but not the noninstalled files such as @samp{make all} would
  184. create).
  185.  
  186. @comment The gratuitous blank line here is to make the table look better
  187. @comment in the printed Make manual.  Please leave it in.
  188. @item clean
  189.  
  190. Delete all files from the current directory that are normally created by
  191. building the program.  Don't delete the files that record the
  192. configuration.  Also preserve files that could be made by building, but
  193. normally aren't because the distribution comes with them.
  194.  
  195. Delete @file{.dvi} files here if they are not part of the distribution.
  196.  
  197. @item distclean
  198. Delete all files from the current directory that are created by
  199. configuring or building the program.  If you have unpacked the source
  200. and built the program without creating any other files, @samp{make
  201. distclean} should leave only the files that were in the distribution.
  202.  
  203. @item mostlyclean
  204. Like @samp{clean}, but may refrain from deleting a few files that people
  205. normally don't want to recompile.  For example, the @samp{mostlyclean}
  206. target for GCC does not delete @file{libgcc.a}, because recompiling it
  207. is rarely necessary and takes a lot of time.
  208.  
  209. @item realclean
  210. Delete everything from the current directory that can be reconstructed
  211. with this Makefile.  This typically includes everything deleted by
  212. @code{distclean}, plus more: C source files produced by Bison, tags tables,
  213. Info files, and so on.
  214.  
  215. One exception, however: @samp{make realclean} should not delete
  216. @file{configure} even if @file{configure} can be remade using a rule in
  217. the Makefile.  More generally, @samp{make realclean} should not delete
  218. anything that needs to exist in order to run @file{configure}
  219. and then begin to build the program.
  220.  
  221. @item TAGS
  222. Update a tags table for this program.
  223.  
  224. @item info
  225. Generate any Info files needed.  The best way to write the rules is as
  226. follows:
  227.  
  228. @smallexample
  229. info: foo.info
  230.  
  231. foo.info: foo.texi chap1.texi chap2.texi
  232.         $(MAKEINFO) $(srcdir)/foo.texi
  233. @end smallexample
  234.  
  235. @noindent
  236. You must define the variable @code{MAKEINFO} in the Makefile.  It should
  237. run the @code{makeinfo} program, which is part of the Texinfo
  238. distribution.
  239.  
  240. @item dvi
  241. Generate DVI files for all TeXinfo documentation.  
  242. For example:
  243.  
  244. @smallexample
  245. dvi: foo.dvi
  246.  
  247. foo.dvi: foo.texi chap1.texi chap2.texi
  248.         $(TEXI2DVI) $(srcdir)/foo.texi
  249. @end smallexample
  250.  
  251. @noindent
  252. You must define the variable @code{TEXI2DVI} in the Makefile.  It should
  253. run the program @code{texi2dvi}, which is part of the Texinfo
  254. distribution.  Alternatively, write just the dependencies, and allow GNU
  255. Make to provide the command.
  256.  
  257. @item dist
  258. Create a distribution tar file for this program.  The tar file should be
  259. set up so that the file names in the tar file start with a subdirectory
  260. name which is the name of the package it is a distribution for.  This
  261. name can include the version number.
  262.  
  263. For example, the distribution tar file of GCC version 1.40 unpacks into
  264. a subdirectory named @file{gcc-1.40}.
  265.  
  266. The easiest way to do this is to create a subdirectory appropriately
  267. named, use @code{ln} or @code{cp} to install the proper files in it, and
  268. then @code{tar} that subdirectory.
  269.  
  270. The @code{dist} target should explicitly depend on all non-source files
  271. that are in the distribution, to make sure they are up to date in the
  272. distribution.  
  273. @xref{Releases, , Making Releases, standards, GNU Coding Standards}.
  274.  
  275. @item check
  276. Perform self-tests (if any).  The user must build the program before
  277. running the tests, but need not install the program; you should write
  278. the self-tests so that they work when the program is built but not
  279. installed.
  280. @end table
  281.  
  282. The following targets are suggested as conventional names, for programs
  283. in which they are useful.
  284.  
  285. @table @code
  286. @item installcheck
  287. Perform installation tests (if any).  The user must build and install
  288. the program before running the tests.  You should not assume that
  289. @file{$(bindir)} is in the search path.  
  290.  
  291. @item installdirs
  292. It's useful to add a target named @samp{installdirs} to create the
  293. directories where files are installed, and their parent directories.
  294. There is a script called @file{mkinstalldirs} which is convenient for
  295. this; find it in the Texinfo package.@c It's in /gd/gnu/lib/mkinstalldirs.
  296. You can use a rule like this:
  297.  
  298. @comment This has been carefully formatted to look decent in the Make manual.
  299. @comment Please be sure not to make it extend any further to the right.--roland
  300. @smallexample
  301. # Make sure all installation directories (e.g. $(bindir))
  302. # actually exist by making them if necessary.
  303. installdirs: mkinstalldirs
  304.         $(srcdir)/mkinstalldirs $(bindir) $(datadir) \
  305.                                 $(libdir) $(infodir) \
  306.                                 $(mandir)
  307. @end smallexample
  308. @end table
  309.  
  310. @node Command Variables
  311. @section Variables for Specifying Commands
  312.  
  313. Makefiles should provide variables for overriding certain commands, options,
  314. and so on.
  315.  
  316. In particular, you should run most utility programs via variables.
  317. Thus, if you use Bison, have a variable named @code{BISON} whose default
  318. value is set with @samp{BISON = bison}, and refer to it with
  319. @code{$(BISON)} whenever you need to use Bison.
  320.  
  321. File management utilities such as @code{ln}, @code{rm}, @code{mv}, and
  322. so on, need not be referred to through variables in this way, since users
  323. don't need to replace them with other programs.
  324.  
  325. Each program-name variable should come with an options variable that is
  326. used to supply options to the program.  Append @samp{FLAGS} to the
  327. program-name variable name to get the options variable name---for
  328. example, @code{BISONFLAGS}.  (The name @code{CFLAGS} is an exception to
  329. this rule, but we keep it because it is standard.)  Use @code{CPPFLAGS}
  330. in any compilation command that runs the preprocessor, and use
  331. @code{LDFLAGS} in any compilation command that does linking as well as
  332. in any direct use of @code{ld}.
  333.  
  334. If there are C compiler options that @emph{must} be used for proper
  335. compilation of certain files, do not include them in @code{CFLAGS}.
  336. Users expect to be able to specify @code{CFLAGS} freely themselves.
  337. Instead, arrange to pass the necessary options to the C compiler
  338. independently of @code{CFLAGS}, by writing them explicitly in the
  339. compilation commands or by defining an implicit rule, like this:
  340.  
  341. @smallexample
  342. CFLAGS = -g
  343. ALL_CFLAGS = -I. $(CFLAGS)
  344. .c.o:
  345.         $(CC) -c $(CPPFLAGS) $(ALL_CFLAGS) $<
  346. @end smallexample
  347.  
  348. Do include the @samp{-g} option in @code{CFLAGS}, because that is not
  349. @emph{required} for proper compilation.  You can consider it a default
  350. that is only recommended.  If the package is set up so that it is
  351. compiled with GCC by default, then you might as well include @samp{-O}
  352. in the default value of @code{CFLAGS} as well.
  353.  
  354. Put @code{CFLAGS} last in the compilation command, after other variables
  355. containing compiler options, so the user can use @code{CFLAGS} to
  356. override the others.
  357.  
  358. Every Makefile should define the variable @code{INSTALL}, which is the
  359. basic command for installing a file into the system.
  360.  
  361. Every Makefile should also define the variables @code{INSTALL_PROGRAM}
  362. and @code{INSTALL_DATA}.  (The default for each of these should be
  363. @code{$(INSTALL)}.)  Then it should use those variables as the commands
  364. for actual installation, for executables and nonexecutables
  365. respectively.  Use these variables as follows:
  366.  
  367. @example
  368. $(INSTALL_PROGRAM) foo $(bindir)/foo
  369. $(INSTALL_DATA) libfoo.a $(libdir)/libfoo.a
  370. @end example
  371.  
  372. @noindent
  373. Always use a file name, not a directory name, as the second argument of
  374. the installation commands.  Use a separate command for each file to be
  375. installed.
  376.  
  377. @node Directory Variables
  378. @section Variables for Installation Directories
  379.  
  380. Installation directories should always be named by variables, so it is
  381. easy to install in a nonstandard place.  The standard names for these
  382. variables are:
  383.  
  384. @table @samp
  385. @item prefix
  386. A prefix used in constructing the default values of the variables listed
  387. below.  The default value of @code{prefix} should be @file{/usr/local}
  388. (at least for now).
  389.  
  390. @item exec_prefix
  391. A prefix used in constructing the default values of some of the
  392. variables listed below.  The default value of @code{exec_prefix} should
  393. be @code{$(prefix)}.
  394.  
  395. Generally, @code{$(exec_prefix)} is used for directories that contain
  396. machine-specific files (such as executables and subroutine libraries),
  397. while @code{$(prefix)} is used directly for other directories.
  398.  
  399. @item bindir
  400. The directory for installing executable programs that users can run.
  401. This should normally be @file{/usr/local/bin}, but write it as
  402. @file{$(exec_prefix)/bin}.
  403.  
  404. @item libdir
  405. The directory for installing executable files to be run by the program
  406. rather than by users.  Object files and libraries of object code should
  407. also go in this directory.  The idea is that this directory is used for
  408. files that pertain to a specific machine architecture, but need not be
  409. in the path for commands.  The value of @code{libdir} should normally be
  410. @file{/usr/local/lib}, but write it as @file{$(exec_prefix)/lib}.
  411.  
  412. @item datadir
  413. The directory for installing read-only data files which the programs
  414. refer to while they run.  This directory is used for files which are
  415. independent of the type of machine being used.  This should normally be
  416. @file{/usr/local/lib}, but write it as @file{$(prefix)/lib}.
  417.  
  418. @item statedir
  419. The directory for installing data files which the programs modify while
  420. they run.  These files should be independent of the type of machine
  421. being used, and it should be possible to share them among machines at a
  422. network installation.  This should normally be @file{/usr/local/lib},
  423. but write it as @file{$(prefix)/lib}.
  424.  
  425. @item includedir
  426. @c rewritten to avoid overfull hbox --roland
  427. The directory for installing header files to be included by user
  428. programs with the C @samp{#include} preprocessor directive.  This
  429. should normally be @file{/usr/local/include}, but write it as
  430. @file{$(prefix)/include}.
  431.  
  432. Most compilers other than GCC do not look for header files in
  433. @file{/usr/local/include}.  So installing the header files this way is
  434. only useful with GCC.  Sometimes this is not a problem because some
  435. libraries are only really intended to work with GCC.  But some libraries
  436. are intended to work with other compilers.  They should install their
  437. header files in two places, one specified by @code{includedir} and one
  438. specified by @code{oldincludedir}.
  439.  
  440. @item oldincludedir
  441. The directory for installing @samp{#include} header files for use with
  442. compilers other than GCC.  This should normally be @file{/usr/include}.
  443.  
  444. The Makefile commands should check whether the value of
  445. @code{oldincludedir} is empty.  If it is, they should not try to use
  446. it; they should cancel the second installation of the header files.
  447.  
  448. A package should not replace an existing header in this directory unless
  449. the header came from the same package.  Thus, if your Foo package
  450. provides a header file @file{foo.h}, then it should install the header
  451. file in the @code{oldincludedir} directory if either (1) there is no
  452. @file{foo.h} there or (2) the @file{foo.h} that exists came from the Foo
  453. package.
  454.  
  455. To tell whether @file{foo.h} came from the Foo package, put a magic
  456. string in the file---part of a comment---and grep for that string.
  457.  
  458. @item mandir
  459. The directory for installing the man pages (if any) for this package.
  460. It should include the suffix for the proper section of the
  461. manual---usually @samp{1} for a utility.  It will normally be
  462. @file{/usr/local/man/man1}, but you should write it as
  463. @file{$(prefix)/man/man1}. 
  464.  
  465. @item man1dir
  466. The directory for installing section 1 man pages.
  467. @item man2dir
  468. The directory for installing section 2 man pages.
  469. @item @dots{}
  470. Use these names instead of @samp{mandir} if the package needs to install man
  471. pages in more than one section of the manual.
  472.  
  473. @strong{Don't make the primary documentation for any GNU software be a
  474. man page.  Write a manual in Texinfo instead.  Man pages are just for
  475. the sake of people running GNU software on Unix, which is a secondary
  476. application only.}
  477.  
  478. @item manext
  479. The file name extension for the installed man page.  This should contain
  480. a period followed by the appropriate digit; it should normally be @samp{.1}.
  481.  
  482. @item man1ext
  483. The file name extension for installed section 1 man pages.
  484. @item man2ext
  485. The file name extension for installed section 2 man pages.
  486. @item @dots{}
  487. Use these names instead of @samp{manext} if the package needs to install man
  488. pages in more than one section of the manual.
  489.  
  490. @item infodir
  491. The directory for installing the Info files for this package.  By
  492. default, it should be @file{/usr/local/info}, but it should be written
  493. as @file{$(prefix)/info}.
  494.  
  495. @item srcdir
  496. The directory for the sources being compiled.  The value of this
  497. variable is normally inserted by the @code{configure} shell script.
  498. @end table
  499.  
  500. For example:
  501.  
  502. @smallexample
  503. @c I have changed some of the comments here slightly to fix an overfull
  504. @c hbox, so the make manual can format correctly. --roland
  505. # Common prefix for installation directories.
  506. # NOTE: This directory must exist when you start the install.
  507. prefix = /usr/local
  508. exec_prefix = $(prefix)
  509. # Where to put the executable for the command `gcc'.
  510. bindir = $(exec_prefix)/bin
  511. # Where to put the directories used by the compiler.
  512. libdir = $(exec_prefix)/lib
  513. # Where to put the Info files.
  514. infodir = $(prefix)/info
  515. @end smallexample
  516.  
  517. If your program installs a large number of files into one of the
  518. standard user-specified directories, it might be useful to group them
  519. into a subdirectory particular to that program.  If you do this, you
  520. should write the @code{install} rule to create these subdirectories.
  521.  
  522. Do not expect the user to include the subdirectory name in the value of
  523. any of the variables listed above.  The idea of having a uniform set of
  524. variable names for installation directories is to enable the user to
  525. specify the exact same values for several different GNU packages.  In
  526. order for this to be useful, all the packages must be designed so that
  527. they will work sensibly when the user does so.
  528.  
  529.